package UIFree.FX.pageturner
{
	import SEApp.se_util;
	
	import UIFree.FX;
	import UIFree.FXEvent;
	import UIFree.uif_ecomponent;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	
	public class pt_basePageTurner extends uif_ecomponent {
		private 	var _startBitmap: Bitmap;
		private 	var _endBitmap: Bitmap;
		private 	var _state: Number;
		private		var _stateFX: FX;
		private		var _stateTransition: Function;
		private		var _stateFps: Number;
		private		var _stateDuration: Number;
		
		public function pt_basePageTurner(parent: uif_ecomponent,
									   a_transition: Function=null,
									   a_fps: Number=20,
									   a_duration: Number=1500) {
			super(parent, 0, 0, parent.width, parent.height);
			_stateTransition = a_transition;
			_stateFps = a_fps;
			_stateDuration = a_duration;
		}
		
		protected function get startBitmap(): Bitmap {
			return _startBitmap;
		}
		
		protected function get endBitmap(): Bitmap {
			return _endBitmap;
		}
		
		public function get state(): Number {
			return _state;
		}
		
		public function set state(a_state: Number): void {
			_state = a_state;
			updateFromState();
		}
		
		public function prepare(startSprite: DisplayObject, 
								endSprite: DisplayObject): void {
			_startBitmap 	= se_util.bmpCopy(startSprite, 1);
			_endBitmap 		= se_util.bmpCopy(endSprite, 1);
/*			
			_startBitmap.x 	= startSprite.x;
			_startBitmap.y 	= startSprite.y;
			_endBitmap.x 	= endSprite.x;
			_endBitmap.y 	= endSprite.y;*/
		}
		
		public function slideFX(startEvent: Function=null,
								endEvent: Function=null): FX {
			function l_doStartFX(e: FXEvent): void {
				startFX();
				if (startEvent != null) startEvent(e);
			} 
			function l_doEndFX(e: FXEvent): void {
				_stateFX.removeEventListener(FXEvent.START, l_doStartFX);
				_stateFX.removeEventListener(FXEvent.START, l_doEndFX);
				endFX();
				if (endEvent != null) endEvent(e);
				_startBitmap.bitmapData.dispose();
				_endBitmap.bitmapData.dispose();
				_stateFX = null;
			} 
			if (_stateFX == null) {
				_stateFX = fx('state', 0, 1, _stateTransition, _stateFps, _stateDuration);
				_stateFX.addEventListener(FXEvent.START, l_doStartFX);
				_stateFX.addEventListener(FXEvent.COMPLETE, l_doEndFX);
				_stateFX.addEventListener(FXEvent.CANCEL, l_doEndFX);
			}
			return _stateFX;
		}
		
		protected function startFX(): void {
		}
		
		protected function endFX(): void {
		}
		
		protected function updateFromState(): void {
		}
	}
}